/* Обнаружение View, Class, CompileInfo */
function DetectComponentImp(){
}


DetectComponentImp.prototype={
	jsTokenaizer:new RegExp(/(\/\*[\s\S.]*?\*\/|[\/]{2,}[^\n\r]*|('|"|\/)((\\\2)|.??)*\2|[*+-\/<>]?={1,2}|[\-+]{2}|(([\-]|\b)\d+\.?\d*|[\-]?\.\d+)(e[\-]?\d+|\b)|[\s\t\n\r]+|[\wæøåêâûîôãõñäëüöÿï]+|[^\wæøåêâûîôãõñäëüöÿï])/gi),
	getStringValue:function(arr,index,count){
		for(var j=index, token; (token = arr[j]) && (count>0); j++)
		{
			if (token.indexOf('"')==0 || token.indexOf("'")==0){
				return token.substring(1,token.length-1);
			}
			count--;
		}
	},
	getViewClassName:function (arr,index){
		var tmp = arr[index];
		if (!tmp){
			return;
		}
		if (tmp == '['){
			return this.getStringValue(arr,index+1,4);
		} else {
			return tmp;
		}
	},
	getCompileInfoName:function(arr,index){
		var BraceCount1 = 0;
		for(var j=index, token; (token = arr[j]); j++)
		{
			switch(token)
			{
				case "Name": if (BraceCount1 == 1){
					return this.getStringValue(arr,j+1,4);
				}
				break;
				case "{" : BraceCount1++;		break;
				case "}" : BraceCount1--;
					if (BraceCount1 == 0){
					 return;
					}
				default:
			}
		}
	},
	search:function(arr,index,position){
		var tmp = arr[index+1];
		if (!tmp || tmp!='.'){
			return;
		}
		var tmp = arr[index+2];
		if (!tmp){
			return;
		}
		var name;		
		switch(tmp){
			case 'Components': tmp = 'c';name = this.getViewClassName(arr,index+3);break;
			case 'Templates': tmp = 't';name = this.getViewClassName(arr,index+3);break;
			case 'CreateCompileInfoByObject': tmp = 'd';name = this.getCompileInfoName(arr,index+3);break;
			default:return;
		}
		if (name != undefined){
			return {
			start:position,
			type:tmp,
			name:name
			};
		}
	},
	scan:function(data){
	    	var arr = data.match(this.jsTokenaizer);
		if (arr){
		var BraceCount = 0;
		var ParenCount = 0;
		var BracketCount = 0;
		var position = 0;
		var result = [];
		var currentObject;
		for(var j=0, token; (token = arr[j]); j++)
		{
		
		switch(token)
		{
			case "{" : BraceCount++;		break;
			case "}" : BraceCount--;
			case ";" : 		
				if (BraceCount==0 && ParenCount==0 && BracketCount==0 && currentObject != undefined){
					currentObject.end=position+1;
					result.push(currentObject);
					currentObject = undefined;
				}
				break;
			case "(" : ParenCount++;		break;
			case ")" : ParenCount--;		break;
			case "[" : BracketCount++;		break;
			case "]" : BracketCount--;		break;
			default  :
				if(BraceCount==0)
				{
					if  (token.indexOf("jsForms") == 0){
						currentObject = this.search(arr,j,position);
					}
				}
			}
			if (token != undefined){
				position+=token.length;
			}
		}
		return result;
	}else{
		return [];
	}
	}
}

var DetectComponent = new DetectComponentImp();

